import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, ManyToOne, JoinColumn } from 'typeorm';
import { ApiProperty } from '@nestjs/swagger';
import { MatchImportConfig } from './match-import-config.entity';

/**
 * 比赛文件导入状态枚举
 */
export enum ImportStatus {
  SUCCESS = 'success',
  FAILED = 'failed',
  PROCESSING = 'processing'
}

/**
 * 比赛文件类型枚举
 */
export enum MatchFileType {
  HOMEPAGE = 'homepage', // 首页文件
  SCORE = 'score'        // 比分文件
}

/**
 * 比赛文件导入记录实体
 */
@Entity('match_import_records')
export class MatchImportRecord {
  @ApiProperty({ description: '记录ID' })
  @PrimaryGeneratedColumn()
  id: number;

  @ApiProperty({ description: '配置ID' })
  @Column({ comment: '配置ID' })
  configId: number;

  @ApiProperty({ description: '文件名' })
  @Column({ length: 255, comment: '文件名' })
  fileName: string;

  @ApiProperty({ description: '文件完整路径' })
  @Column({ length: 500, comment: '文件完整路径' })
  filePath: string;

  @ApiProperty({ description: '文件类型', enum: MatchFileType })
  @Column({ 
    type: 'enum', 
    enum: MatchFileType, 
    comment: '文件类型：homepage-首页文件，score-比分文件' 
  })
  fileType: MatchFileType;

  @ApiProperty({ description: '导入状态', enum: ImportStatus })
  @Column({ 
    type: 'enum', 
    enum: ImportStatus, 
    comment: '导入状态：success-成功，failed-失败，processing-处理中' 
  })
  status: ImportStatus;

  @ApiProperty({ description: '错误信息' })
  @Column({ type: 'text', nullable: true, comment: '错误信息' })
  errorMessage?: string;

  @ApiProperty({ description: '导入时间' })
  @CreateDateColumn({ comment: '导入时间' })
  importTime: Date;

  @ApiProperty({ description: '重试次数' })
  @Column({ default: 0, comment: '重试次数' })
  retryCount: number;

  @ApiProperty({ description: '文件日期（从文件名解析）' })
  @Column({ type: 'date', nullable: true, comment: '文件日期（从文件名解析）' })
  fileDate?: Date;

  // 关联配置
  @ManyToOne(() => MatchImportConfig)
  @JoinColumn({ name: 'configId' })
  config: MatchImportConfig;
}